import { useEffect, useState } from "react";

export default function useTime() {
  const [now, setNow] = useState(new Date());

  useEffect(() => {
    let timer: ReturnType<typeof setTimeout>;
    function update() {
      timer = setTimeout(() => {
        setNow(new Date());
        update();
      }, 500);
    }

    update();

    return () => {
      clearTimeout(timer);
    };
  });

  return `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}`;
}
